package com.lifa.controller;

import com.lifa.dao.RolePermissionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/rolepermission")
public class RolePermissionController {
    @Autowired
    private RolePermissionDao rolePermissionDao ;
    @RequestMapping("/grantPermission")
    @ResponseBody
    public Map<String,Object> grantPermission(int roleid,String pmid){
        Map<String,Object> map = new HashMap<>() ;
    //    System.out.println(roleid + ">>" + pmid);
        try {
            String[] ss = pmid.split("-") ;
            int[] perids = new int[ss.length-1] ;
            for(int i=0;i<perids.length;i++){
                perids[i] = Integer.parseInt(ss[i+1]) ;
            }
            //删除角色对应的权限
            rolePermissionDao.del(roleid);
            //增加用户选 择的权限
            for(int permissionid : perids) {
                rolePermissionDao.save(roleid, permissionid);
            }
            map.put("msg","1") ;
        } catch (NumberFormatException e) {
            map.put("msg","2") ;
            e.printStackTrace();
        }


        return map ;
    }
}
